﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aspfm.Dal;
using Aspfm.Main;

namespace Aspfm.DataBase
{
    public partial class UcMSSql : UserControl
    {
        public UcMSSql()
        {
            InitializeComponent();

            textBox2.KeyUp += new KeyEventHandler(textBox2_KeyUp);
            textBox3.KeyUp += new KeyEventHandler(textBox2_KeyUp);
            textBox4.KeyUp += new KeyEventHandler(textBox2_KeyUp);
            textBox5.KeyUp += new KeyEventHandler(textBox2_KeyUp);
            textBox6.KeyUp += new KeyEventHandler(textBox2_KeyUp);

            textBox2_KeyUp(null, null);
        }

        void textBox2_KeyUp(object sender, KeyEventArgs e)
        {
            DataSource data = GetData("");
            if (textBox7.ReadOnly)
            {
                textBox7.Text = DefSql(data.IP, data.dataSourceName, data.User, data.Pwd);
            }
        }

        public DataSource GetData(string key)
        {
            DataSource data = new DataSource();
            data.Key = key;
            data.IP = textBox2.Text.Trim();
            data.port = textBox3.Text.Trim();
            data.dataSourceName = textBox4.Text.Trim();
            data.User = textBox5.Text.Trim();
            data.Pwd = textBox6.Text.Trim();
            data.datatype = DataSourceType.MSSql;
            data.IsChange = checkBox1.Checked;
            data.ConnStr = textBox7.Text.Trim();
            return data;
        }
        public void Clear()
        {
            textBox2.Text = "127.0.0.1";
            textBox3.Text = "";
            textBox4.Text = "Aspfm";
            textBox5.Text = "sa";
            textBox6.Text = "123";
            checkBox1.Checked = false;

            textBox2_KeyUp(null, null);
        }
        public void SetData(DataSource data)
        {
            if (!string.IsNullOrWhiteSpace(data.IP))
            {
                textBox7.Text = data.ConnStr;
                textBox2.Text = data.IP;
                textBox3.Text = data.port;
                textBox4.Text = data.dataSourceName;
                textBox5.Text = data.User;
                textBox6.Text = data.Pwd;
                checkBox1.Checked = data.IsChange;
            }
            else
            {
                Clear();
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            textBox7.ReadOnly = !checkBox1.Checked;
            textBox2.ReadOnly = checkBox1.Checked;
            textBox3.ReadOnly = checkBox1.Checked;
            textBox4.ReadOnly = checkBox1.Checked;
            textBox5.ReadOnly = checkBox1.Checked;
            textBox6.ReadOnly = checkBox1.Checked;
        }

        private string DefSql(string ip,string dataname, string user, string pwd)
        {
            return @"Data Source=" + ip + ";Initial Catalog=" + dataname + ";"+
                "User ID=" + user + ";Password=" + pwd + ";";
        }
    }
}
